//
//  SampleBookMainView.m
//  Zinipad
//
//  Created by ZeLkOvA on 12. 5. 17..
//  Copyright (c) 2012년 __MyCompanyName__. All rights reserved.
//

#import "SampleBookViewWallpaper.h"
#import "MyTreeViewCell.h"
#import "TreeView.h"
#import "iCarousel.h"
#import "DatabaseManager.h"

//#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

//#define NUMBER_OF_ITEMS 6
#define NUMBER_OF_VISIBLE_ITEMS 5
#define ITEM_SPACING 60.0f
#define INCLUDE_PLACEHOLDERS YES

@interface SampleBookViewWallpaper () <UIActionSheetDelegate>

@property (nonatomic, assign) BOOL wrap;
@property (nonatomic, retain) NSMutableArray *items;

@end

@implementation SampleBookViewWallpaper

@synthesize carousel;
@synthesize wrap;
@synthesize items;
@synthesize bottomListScroll;
@synthesize ColorImgArray;
@synthesize detailImageView;
@synthesize carouselColor;
@synthesize patternView;
@synthesize selectedImgList;
@synthesize detailDescriptionBg;
@synthesize selectedImgOrigin;
@synthesize WideButton;
@synthesize MatchingButton;
@synthesize PatternButton;

- (void)setUpImg
{
    //set up data
    wrap = YES;
//    self.items = [NSMutableArray array];
//
//        // 이미지 가져와서 저장하기 
//    [items addObject:@"x.png"];
//    [items addObject:@"y.png"];
//    [items addObject:@"z.png"];
//    [items addObject:@"x.png"];
//    [items addObject:@"y.png"];
//    [items addObject:@"z.png"];
//    [items addObject:@"x.png"];
//    [items addObject:@"y.png"];
//    [items addObject:@"z.png"];
//    [items addObject:@"x.png"];
//    [items addObject:@"y.png"];
//    [items addObject:@"z.png"];
}

-(void)SettingColorImg
{
    self.ColorImgArray = [NSMutableArray array];
    [ColorImgArray addObject:@"color_19.png"];
    [ColorImgArray addObject:@"color_09.png"];
    [ColorImgArray addObject:@"color_11.png"];
    [ColorImgArray addObject:@"color_06.png"];
    [ColorImgArray addObject:@"color_02.png"];
    [ColorImgArray addObject:@"color_17.png"];
    [ColorImgArray addObject:@"color_13.png"];
    [ColorImgArray addObject:@"color_04.png"];
    [ColorImgArray addObject:@"color_09.png"];
    [ColorImgArray addObject:@"color_10.png"];
    [ColorImgArray addObject:@"color_15.png"];
    [ColorImgArray addObject:@"color_05.png"];
    [ColorImgArray addObject:@"color_12.png"];
    [ColorImgArray addObject:@"color_14.png"];
    [ColorImgArray addObject:@"color_08.png"];
    [ColorImgArray addObject:@"color_07.png"];
    [ColorImgArray addObject:@"color_01.png"];
//    [ColorImgArray addObject:@"color_18.png"];
//    [ColorImgArray addObject:@"color_01.png"];

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        bFirstLoad = YES;
        [self SettingColorImg];
        [self setUpImg];
      
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

-(void)categoryIndexReceive:(int)_nCount
{
    nCategoryIndex = _nCount;
 
       
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
                                  
    firstLoad = false;
    bWide = false;
    UIImage* BGSampleBookImage = [UIImage imageNamed:@"bg_samplebook"];
    UIImageView* BGSampleBookImageView  = [[UIImageView alloc] initWithImage:BGSampleBookImage];
    [BGSampleBookImageView setFrame:CGRectMake(0, 0, BGSampleBookImage.size.width, BGSampleBookImage.size.height)];
    [self.view addSubview:BGSampleBookImageView];
    

    
    [self InitColorCarousel];
    [self InitCarousel];
    
//    bottomListScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 550, self.view.frame.size.width, 130)];
//    [bottomListScroll setBackgroundColor:[UIColor blackColor]];
//
//    [self.view addSubview:bottomListScroll];
    
    ///////////////////
    // 관련 바닥재 이미지 불러와야함
    //////////////////
    
    UIImage* tileBGImage = [UIImage imageNamed:[self.items objectAtIndex:0]];
    detailImageView = [[UIImageView alloc] initWithImage:tileBGImage];
    [detailImageView setFrame:CGRectMake(97, 7, 515, 636)];
    
    detailDescriptionBg = [[UIImageView alloc] init];
    [detailDescriptionBg setBackgroundColor:[UIColor blackColor]];
    [detailDescriptionBg setFrame:CGRectMake(0, 636-50, 515, 50)];
    [detailDescriptionBg setAlpha:0.0f];
    [detailImageView addSubview:detailDescriptionBg];
    [self.view addSubview:detailImageView];

    WideButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [WideButton setFrame:CGRectMake(250,642-45, 40, 40)];
    [WideButton setTitle:[NSString stringWithFormat:@""] forState:UIControlStateNormal];
    [WideButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [WideButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
//    [WideButton setTitleEdgeInsets:UIEdgeInsetsMake(5, 10, 5, 5)];
    [WideButton addTarget:self action:@selector(WideButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view  addSubview:WideButton];
    
    MatchingButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [MatchingButton setFrame:CGRectMake(450,642-45, 40, 40)];
    [MatchingButton setTitle:[NSString stringWithFormat:@"매칭"] forState:UIControlStateNormal];
    [MatchingButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [MatchingButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
//    [MatchingButton setTitleEdgeInsets:UIEdgeInsetsMake(5, 10, 5, 5)];
    [MatchingButton addTarget:self action:@selector(MatchingButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view  addSubview:MatchingButton];
    
    PatternButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [PatternButton setFrame:CGRectMake(510, 642-45, 40, 40)];
    [PatternButton setTitle:[NSString stringWithFormat:@"패턴"] forState:UIControlStateNormal];
    [PatternButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [PatternButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
//    [PatternButton setTitleEdgeInsets:UIEdgeInsetsMake(5, 10, 5, 5)];
    [PatternButton addTarget:self action:@selector(PatternButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view  addSubview:PatternButton];
    
    //    
//    for (int i =0; i<[items count]; i++)
//    {
//        
//        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//        [button addTarget:self action:@selector(imageTouch:withEvent:) forControlEvents:UIControlEventTouchDown];
//        [button addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
//        [button addTarget:self action:@selector(imageTouchUp:withEvent:) forControlEvents:UIControlEventTouchUpInside];
//        [button setImage:[UIImage imageNamed:[items objectAtIndex:i]] forState:UIControlStateNormal];
//        [button setFrame:CGRectMake(10+i*240, 10, 200, 100)];
//        [button setTag:i];
//        [bottomListScroll setContentSize:CGSizeMake(250*[items count],130)];
//        [bottomListScroll addSubview:button];
//    }

}
-(void)WideButtonPressed:(id)sender
{
    if(bWide == false)
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    
        [detailDescriptionBg setFrame:CGRectMake(0, 636-200, 515, 200)];
    
        [WideButton setFrame:CGRectMake(250,642-200, 40, 40)];

//        [MatchingButton setFrame:CGRectMake(450,642-200, 40, 40)];
//        [PatternButton setFrame:CGRectMake(510, 642-200, 40, 40)];
        [UIView commitAnimations];
        bWide = true;
        
    }
    else 
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        
        [detailDescriptionBg setFrame:CGRectMake(0, 636-50, 515, 50)];
        
        [WideButton setFrame:CGRectMake(250,642-45, 40, 40)];
//        [MatchingButton setFrame:CGRectMake(450,642-45, 40, 40)];
//        [PatternButton setFrame:CGRectMake(510, 642-45, 40, 40)];
         [UIView commitAnimations];
        bWide = false;

    }
}

- (void) imageMoved:(id) sender withEvent:(UIEvent *) event
{
    CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
    UIControl *control = sender;
    control.center = point;
}

- (void) imageTouch:(id) sender withEvent:(UIEvent *) event
{
//    CGPoint point = [[[event allTouches] anyObject] locationInView:bottomListScroll];
    CGPoint point2 = [[[event allTouches] anyObject] locationInView:self.view];
    UIControl *control = sender;
    control.center = point2;
    [self.view addSubview:control];

}
- (void) imageTouchUp:(id) sender withEvent:(UIEvent *) event
{
    CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
    UIControl *control = sender;
    control.center = point;
    int aa = [sender tag];
    
//    [self.view addSubview:control];
    NSLog(@"%f",point.x);
    NSLog(@"%f",point.y);
       if(point.x > 20 && point.x < 600)
        {
            if(point.y > 300 && point.y < 500)
           {
               [self.view addSubview:control];
                [control removeFromSuperview ];
               NSLog(@"Come Inside");
//               [control setFrame:CGRectMake(10+240*aa,10,200,100)];
//               [bottomListScroll addSubview:control];
               
               ////////////////////
               // 바닥재 이미지 들어가기
               
               ////////////////////

//                return;
           }
        }

    [control setFrame:CGRectMake(10+240*aa,10,200,100)];
    [bottomListScroll addSubview:control];
    
}

-(void)InitCarousel
{
    carousel = [[iCarousel alloc] init];
    [carousel setFrame:CGRectMake(590,170,150,300)];
    [self.view addSubview:carousel];
    
    carousel.delegate = self;
    carousel.dataSource = self;
    //configure carousel
    carousel.type = iCarouselTypeInvertedWheel;
    //    navItem.title = @"CoverFlow2";
    carousel.vertical = !carousel.vertical;
}

-(void)InitColorCarousel
{
//    carouselColor
    carouselColor = [[iCarouselColor alloc] init];
    [carouselColor setFrame:CGRectMake(-20,170,150,300)];
    carouselColor.dataSourceColor = self;
    carouselColor.delegate = self;
    [self.view addSubview:carouselColor];
    //    carouselColor.delegate = self;
    //    carouselColor.dataSourceColor = self;
    carouselColor.type = iCarouselColorTypeWheel;
    carouselColor.vertical = !carouselColor.vertical;
//    ColorView = [[SampleBookColorViewWallpaper alloc] init];
//    [ColorView.view setFrame:CGRectMake(40,100,150,300)];
//    [self.view addSubview:ColorView.view];

}

-(void)MatchingButtonPressed:(id)sender
{
    
}
-(void)PatternButtonPressed:(id)sender
{
    [self PatternView:@"파일명"];
}
- (void)carouselCurrnetImgIndex:(int)_imgIndex;
{
    NSLog(@"ImgIndex == %d",_imgIndex);
    UIImage* tileBGImage = [UIImage imageNamed:[selectedImgOrigin objectAtIndex:_imgIndex]];
    [detailImageView setImage:tileBGImage];
    [detailImageView setAlpha:1.0f];
    [detailDescriptionBg setAlpha:0.6f];

}

- (void)carouselCurrnetImgIndexColor:(int)_imgIndex;
{
    if(firstLoad == false)
    {
        firstLoad = true;
        return;
    }
    NSLog(@"ImgIndexColor %%    == %d",_imgIndex);
    if(nCategoryIndex == 0)
    {
        
    }
    selectedImgList = [[NSMutableArray alloc] init];
    selectedImgList = [DatabaseManager selectedColorList:_imgIndex category:nCategoryIndex];
    if([selectedImgList count] == 0)
    {
        [carousel removeFromSuperview];
        [detailImageView setAlpha:0.0f];
        [detailDescriptionBg setAlpha:0.0f];
//        if(carousel)
//        {
//            [carousel removeFromSuperview];
//            [carousel release];
//        }
        return;
    }
    self.items = [NSMutableArray array];
    selectedImgOrigin = [[NSMutableArray alloc] init];
    for (int i = 0; i<[selectedImgList count]; i++) 
    {
        NSMutableDictionary* dic = [NSMutableDictionary dictionary];
        dic = [selectedImgList objectAtIndex:i];
        NSString* thumbImg = [dic objectForKey:@"W_img_thumb"];
        [self.items addObject:thumbImg];
        NSString* originImg = [dic objectForKey:@"W_img_original"];
        [selectedImgOrigin addObject:originImg];
    }
    if(carousel)
    {
        [carousel removeFromSuperview];
        [carousel release];
    }
    [self InitCarousel];
    
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return [items count];
}

- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
{
    //limit the number of items views loaded concurrently (for performance reasons)
    //this also affects the appearance of circular-type carousels
    return NUMBER_OF_VISIBLE_ITEMS;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    if (view == nil)
    {
//      NSLog(@"here %d",index);
      view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:[items objectAtIndex:index]]] autorelease];
      [view setFrame:CGRectMake(0, 0, 70, 70)];
      view.layer.doubleSided = YES; //prevent back side of view from showing
    
//        label = [[[UILabel alloc] initWithFrame:view.bounds] autorelease];
//        label.backgroundColor = [UIColor clearColor];
//        label.textAlignment = UITextAlignmentCenter;
//        label.font = [label.font fontWithSize:10];
//        label.text =  [NSString stringWithFormat:@"%d",count];
//        [view addSubview:label];
//        count++;
    }
    else
    {
//        NSLog(@"here %d",index);
        view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:[items objectAtIndex:index]]] autorelease];
        [view setFrame:CGRectMake(0, 0, 70, 70)];
        view.layer.doubleSided = YES; 
      
    }
//   
    return view;
}

- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
{
    //note: placeholder views are only displayed on some carousels if wrapping is disabled
    return 0;
}


- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
    //usually this should be slightly wider than the item views
    return ITEM_SPACING;
}

- (BOOL)carouselShouldWrap:(iCarousel *)carousel
{
    return wrap;
}

// 패던보기

-(void)PatternView:(NSString*)_imgFile
{
    patternView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
    [patternView setBackgroundColor:[UIColor whiteColor]];
    
    UIButton* PatternBackButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [PatternBackButton setFrame:CGRectMake(1024 - 70, 30, 70, 70)];
    [PatternBackButton setTitle:[NSString stringWithFormat:@"X"] forState:UIControlStateNormal];
    [PatternBackButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [PatternBackButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [PatternBackButton setTitleEdgeInsets:UIEdgeInsetsMake(5, 10, 5, 5)];
    [PatternBackButton addTarget:self action:@selector(PatternBackButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [patternView addSubview:PatternBackButton];
    
    [self.view.superview addSubview:patternView];

}
-(void)PatternBackButtonPressed:(id)sender
{
    [patternView removeFromSuperview];
    [patternView release];
}

-(void)dealloc
{
    carousel.delegate = nil;
    carousel.dataSource = nil;
    [carousel release];
    [items release];
    [super dealloc];
}

#pragma mark - Button Handler


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return YES;
}

- (NSUInteger)numberOfItemsInCarouselColor:(iCarouselColor *)carousel
{
    return [ColorImgArray count];
}

- (NSUInteger)numberOfVisibleItemsInCarouselColor:(iCarouselColor *)carousel
{
    //limit the number of items views loaded concurrently (for performance reasons)
    //this also affects the appearance of circular-type carousels
    return NUMBER_OF_VISIBLE_ITEMS;
}

- (UIView *)carouselColor:(iCarouselColor *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    if (view == nil)
    {
//        NSLog(@"here %d",index);
        view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:[ColorImgArray objectAtIndex:index]]] autorelease];
        [view setFrame:CGRectMake(0, 0, 70, 70)];
        view.layer.doubleSided = YES; //prevent back side of view from showing
        
        //        label = [[[UILabel alloc] initWithFrame:view.bounds] autorelease];
        //        label.backgroundColor = [UIColor clearColor];
        //        label.textAlignment = UITextAlignmentCenter;
        //        label.font = [label.font fontWithSize:10];
        //        label.text =  [NSString stringWithFormat:@"%d",count];
        //        [view addSubview:label];
        //        count++;
    }
    else
    {
//        NSLog(@"here %d",index);
        view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:[ColorImgArray objectAtIndex:index]]] autorelease];
        [view setFrame:CGRectMake(0, 0, 70, 70)];
        view.layer.doubleSided = YES; 
        
    }
    //   
    return view;
}

- (NSUInteger)numberOfPlaceholdersInCarouselColor:(iCarouselColor *)carousel
{
    //note: placeholder views are only displayed on some carousels if wrapping is disabled
    return 0;
}


- (CGFloat)carouselItemWidthColor:(iCarouselColor *)carousel
{
    //usually this should be slightly wider than the item views
    return ITEM_SPACING;
}

- (BOOL)carouselShouldWrapColor:(iCarouselColor *)carousel
{
    return wrap;
}


@end

